<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{   
    //商品首页
    public function getIndex(Request $request)
    {
         //判断是否有查询条件
        if($request->input('keywords')){
            $goods = DB::table('goods')->where('gname','like','%'.$request->input('keywords').'%')->get();
        }else{
            $goods = DB::table('goods')->get();
        }

        $list = $request->all();
        //解析模板
        return view('admin.goods.index',['goods'=>$goods,'list'=>$list]);
    }

    //商品添加
    public function getAdd()
    {
    	$cates = CateController::getCates();

    	//解析模板
    	return view('admin.goods.add',['cates'=>$cates]);
    }

    //商品添加 处理数据
    public function postInsert(Request $request)
    {
    	//提取数据
    	$data = $request->only('gname','price','cnt','status','gdesc','tid');

    	//文件上传
    	$data['gpic'] = $this->upload($request,'gpic');

    	$res = DB::table('goods')->insert($data);

    	if($res){
    		//上传成功
    		return redirect('/admin/goods/index')->with('success','商品添加成功');
    	}else{
    		//上传失败
    		return back()->with('error','商品添加失败');
    	}

    }

    //封装方法 进行ajax删除
    public function postDelete(Request $request)
    {
    	//获取id
    	$gid = $request->input('gid');

    	$res = DB::table('goods')->where('gid',$gid)->delete();

    	echo $res;
    }

    //封装方法 进行修改显示
    public function getEdit(Request $request)
    {
    	$cates = CateController::getCates();

        //获取tid
        $tid = $request->input('tid');
        // 判断顶级不可修改
        if($tid == 0){
            return redirect('/admin/goods/index');
        }
        $arr = DB::table('cate')->where('tid',$tid)->first();
        
    	//获取gid
    	$gid = $request->input('gid');
    	//查询单条
    	$goods = DB::table('goods')->where('gid',$gid)->first();
    	//dd($goods);
    	//解析模板
    	return view('/admin/goods/edit',['goods'=>$goods,'cates'=>$cates,'arr'=>$arr]);
    }

    //封装方法 进行数据修改
    public function postUpdate(Request $request)
    {   
        //获取数据
        $data = $request->only('gname','price','cnt','status','gdesc','tid');
        //判断文件上传
        if($request->hasFile('gpic'))
        {
            $data['gpic'] = $this->upload($request,'gpic');
        }
        //获取gid
        $gid = $request->input('gid');
        //进行数据修改操作
        $res = DB::table('goods')->where('gid',$gid)->update($data);

        if($res){
            //修改成功
            return redirect('/admin/goods/index')->with('success','商品修改成功');
        }else{
            //修改失败
            return back()->with('error','商品修改失败');
        }
    }


    //封装方法 文件上传
    private function upload($request,$filename)
    {
    	if($request->hasFile($filename)){
    		//获取文件后缀名
    		$suffix = $request->file($filename)->getClientOriginalExtension();
    		//随机文件名
    		$name = md5(time().rand(1,9999));
    		$request->file($filename)->move('./upload/',$name.'.'.$suffix);
    		return '/upload/'.$name.'.'.$suffix;
    	}else{
    		echo '没有文件上传';
    	}
    }
}
